$(document).ready(function () {
    $('#btn-reupload-header').hide();
    layui.use(['form', 'upload'], function () {
        var form = layui.form;
        var upload = layui.upload;
        form.render();
        initUpload(upload);
    });
});

function initUpload(upload) {
    // 执行实例
    var uploadInst = upload.render({
        elem: '#btn-upload-header'
        , method: 'POST'
        , accept: 'image'
        , exts: 'jpg|png|gif'
        , size: 100
        , url: '/upload'
        , before: function(obj) {
            obj.preview(function(index, file, result) {
                $('#img-header').addClass('layui-circle')
                $('#img-header').attr('src', result);
            });
        }
        , done: function (res, index, upload) {
            // 上传完毕回调
            // 如果上传失败

        }
        , error: function () {
            // 请求异常回调
            var re_upload = $('#re-upload-content');
            $('#btn-reupload-header').show();
            re_upload.find('#btn-reupload-header').on('click', function() {
                uploadInst.upload();
            });
        }
    });
}